import requst from '../../../util/Http.js'
Page({
  data:{
    address:{
      name:'',
      phone:'',
      detail:''
    }
  },
  onLoad(){    
    let self=this;
    wx.getStorage({
      key: 'address',
      success: function(res){
        self.setData({
          address : res.data
        })
      }
    })
  },
  formSubmit(){
    var self = this;
    if(self.data.address.name && self.data.address.phone && self.data.address.detail){
      let user=wx.getStorageSync('userinfo')
      let obj={user_id:user.id,name:self.data.address.name,mobile:self.data.address.phone,detail:self.data.address.detail}
      requst('Index/addOrupdateress',obj,'post').then(res=>{
        if(res.code==200){
          wx.showToast({
            title: '保存成功',
          })
          let obj={
            id: res.data.id,
            name: res.data.name,
            phone: res.data.mobile,
            detail: res.data.detail
          }
          wx.setStorage({
            key: 'address',
            data:obj,
            success(){
              wx.navigateBack();
            }
          })
        }
      })
   
    }else{
      wx.showModal({
        title:'提示',
        content:'请填写完整资料',
        showCancel:false
      })
    }
  },
  bindName(e){
    this.setData({
      'address.name' : e.detail.value
    })
  },
  bindPhone(e){
    this.setData({
      'address.phone' : e.detail.value
    })
  },
  bindDetail(e){
    this.setData({
      'address.detail' : e.detail.value
    })
  }
})